package com.bflow.impl;

import com.bflow.api.IBflowDefineActRouteService;
import com.bflow.api.IBflowDefineActService;
import com.bflow.api.IBflowDefineProcessService;
import com.bflow.api.bo.BflowDefineActBo;
import com.bflow.api.bo.BflowDefineActRouteBo;
import com.bflow.api.bo.BflowDefineProcessBo;
import com.bflow.api.constant.BflowConstant;
import com.bflow.api.constant.BflowConstant.*;
import com.bflow.commons.consts.CommConst;
import com.bflow.commons.exception.BizException;
import com.bflow.commons.service.BaseService;
import com.bflow.commons.util.StrUtil;
import com.bflow.dao.BflowDefineActRouteDao;
import com.bflow.dao.po.BflowDefineActRoutePo;
import com.bflow.util.AssertUtil;
import com.bflow.util.BeanMapper;
import com.bflow.util.JsonUtil;
import com.bflow.util.SysSeqUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 功能：BflowDefineActRoute 活动路由-实现业务接口
 * 作者：haowen
 * 日期：2018-12-13
 */
@Service("bflowDefineActRouteService")
public class BflowDefineActRouteServiceImpl extends BaseService<BflowDefineActRoutePo, BflowDefineActRouteBo> implements IBflowDefineActRouteService<BflowDefineActRoutePo, BflowDefineActRouteBo> {


	@Autowired
	private BflowDefineActRouteDao dao;
	@Autowired
	private IBflowDefineActService<?, BflowDefineActBo> bflowDefineActService;
	@Autowired
	private IBflowDefineProcessService<?, BflowDefineProcessBo> bflowDefineProcessService;

	@Override
	protected BflowDefineActRouteDao getDao() {
		return dao;
	}

	@Override
	public void add(BflowDefineActRouteBo bo) {
		bo.setId(SysSeqUtil.genId());
		bo.setCreateTime(new Date());
		BflowDefineActRoutePo po = BeanMapper.map(bo, BflowDefineActRoutePo.class);
		if (dao.insert(po) <= 0) {
			throw new BizException("新增失败");
		}
	}

	@Override
	public void batchInsert(List<BflowDefineActRouteBo> entities, String procId, Integer version) {
		AssertUtil.isNotEmpty(entities, "批量插入活动路由失败, 参数不能为空");
		Date now = new Date();
		for (BflowDefineActRouteBo routeBo : entities) {
			routeBo.setId(SysSeqUtil.genId());
			routeBo.setProcId(procId);
			routeBo.setStatus(CommConst.STATUS_Y);
			routeBo.setCreateTime(now);
			routeBo.setVersion(version);
		}
		super.batchInsert(entities);
	}

	@Override
	public void batchDel(String procId, Integer version) throws BizException {
		Map param = new HashMap();
		param.put("procId", procId);
		param.put("version", version);
		param.put("updateTime", new Date());
		dao.batchDel(param);
	}


	@Override
	public List<BflowDefineActRouteBo> findListByProcKey(String procKey, BflowConstant.CommStatus status) throws BizException {
		AssertUtil.isNotEmpty(procKey, "活动key不能为空");
		if(status==null){
			status=BflowConstant.CommStatus.Y;
		}
        List<BflowDefineActRouteBo> list = dao.findListByProcKey(procKey, Integer.parseInt(status.getCode()));
		return list;
	}

	@Override
	public List<BflowDefineActRouteBo> findListByProcId(String actId, String procId, CommStatus status) throws BizException {
		BflowDefineActRouteBo queryRouteBo = new BflowDefineActRouteBo();
		queryRouteBo.setActId(actId);
		queryRouteBo.setProcId(procId);
		queryRouteBo.setStatus(status.getCode());
		List<BflowDefineActRouteBo> actRouteBos = getListBy(queryRouteBo);
		return actRouteBos;
	}

	@Override
	public List<BflowDefineActRouteBo> findListByProcId(String procId, Integer version) throws BizException {
		BflowDefineActRouteBo queryBo = new BflowDefineActRouteBo();
		queryBo.setStatus(CommStatus.Y.getCode());
		queryBo.setProcId(procId);
		if(version != null)
			queryBo.setVersion(version);
		queryBo.setStatus(CommStatus.Y.getCode());
		List<BflowDefineActRouteBo> list = getListBy(queryBo);
		if(CollectionUtils.isNotEmpty(list)){
			BflowDefineActBo actBo = null;
			for (BflowDefineActRouteBo routeBo : list) {
				if(StrUtil.isNotBlank(routeBo.getNextActId())){
					actBo = bflowDefineActService.findById(routeBo.getNextActId());
					if(actBo != null){
						routeBo.setActName(actBo.getName());
						routeBo.setAssignee(actBo.getAssignee());
					}
				}
			}
		}
		return list;
	}

	@Override
	public List<BflowDefineActRouteBo> findListByProcId(String procId) throws BizException {
		return findListByProcId(procId, null);
	}

	@Override
	public BflowDefineActRouteBo getRouteItemById(String routeId) throws BizException {
		AssertUtil.isNotEmpty(routeId, "活动key不能为空");
		BflowDefineActRouteBo query=new BflowDefineActRouteBo();
		query.setId(routeId);
		query.setStatus(BflowConstant.CommStatus.Y.getCode());
		BflowDefineActRouteBo item = this.getOneBy(query);
		return item;
	}

	/**
	 * 定义按活动顺序设置的默认活动路由
	 * @param actSet 活动集合
	 * @param procId 流程ID
	 * @param version 流程版本
	 * @throws BizException
	 */
	@Transactional
	@Override
	public void setDefaultActRoute(String[] actSet, String procId, Integer version) throws BizException{
		AssertUtil.isNotEmpty(actSet, "活动集合 不能为空");
		//定义活动路由
		List<BflowDefineActRouteBo> routeBos = new ArrayList<>();
		for(int i = 0; i < actSet.length ; i++){
			String actId = actSet[i];
			AssertUtil.isTrue(StringUtils.isNotBlank(actId), "活动集合内的活动ID 不能为空");

			//审批通过
			BflowDefineActRouteBo passRouteBo = new BflowDefineActRouteBo();
			passRouteBo.setId(SysSeqUtil.genId());
			passRouteBo.setActSort(i);
			passRouteBo.setActId(actId);
			passRouteBo.setCondKey(CommDefineActRouteCondDesc.DEF_PASS.getCode());
			passRouteBo.setCondDesc(CommDefineActRouteCondDesc.DEF_PASS.getTitle());
			//设置下个活动ID
			if(i != actSet.length-1 ){
				String nextActId = actSet[i + 1];
				AssertUtil.isTrue(StringUtils.isNotBlank(nextActId), "活动集合内的活动ID 不能为空");
				passRouteBo.setNextActId(nextActId);
			}
			passRouteBo.setStatus(CommConst.STATUS_Y);
			passRouteBo.setCreateTime(new Date());
			passRouteBo.setUpdateTime(new Date());
			passRouteBo.setProcId(procId);
			passRouteBo.setVersion(version);

			//审批不通过
			BflowDefineActRouteBo noPassRouteBo = new BflowDefineActRouteBo();
			noPassRouteBo.setId(SysSeqUtil.genId());
			noPassRouteBo.setActSort(i);
			noPassRouteBo.setActId(actId);
			noPassRouteBo.setCondKey(CommDefineActRouteCondDesc.DEF_PASS.getCode());
			noPassRouteBo.setCondDesc(CommDefineActRouteCondDesc.DEF_NOPASS.getTitle());
			noPassRouteBo.setStatus(CommConst.STATUS_Y);
			noPassRouteBo.setCreateTime(new Date());
			noPassRouteBo.setUpdateTime(new Date());
			noPassRouteBo.setProcId(procId);
			noPassRouteBo.setVersion(version);

			routeBos.add(passRouteBo);
			routeBos.add(noPassRouteBo);
		}

		AssertUtil.isTrue(CollectionUtils.isNotEmpty(routeBos), "活动路由 不能为空");
		System.out.println("活动路由集合：" + JsonUtil.toStr(routeBos));
		//保存活动路由
        this.batchInsert(routeBos);
	}

}
